
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>留言板V1.0</title>

    <!-- Bootstrap core CSS -->
    <link href="<?php echo STATIC_PATH ?>/css/bootstrap.min.css" rel="stylesheet">
  </head>

  <body>
  
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">留言板V1.0</a>
        </div>
      </div>
    </nav>

    <!-- Main jumbotron for a primary marketing message or call to action -->
    <style>
        .info .error{
          color: red;
        }
        .info .ok{
          color: green;
        }
    </style>
    <div class="container" style="padding-top: 0px;">
      <!-- Example row of columns -->
      <div class="row">
      </div>
      <div class="box" style="margin-top: 100px;">
        <form class="form-horizontal" id="reg_form">
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">用户名</label>
            <div class="col-sm-4">
              <input type="text" name="username" class="form-control" id="username" >
            </div>
            <div class="col-sm-4 info">
               
            </div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">手机</label>
            <div class="col-sm-4">
              <input type="text" id="phone" name="phone" class="form-control" >
            </div>
            <div class="col-sm-4 info"></div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
            <div class="col-sm-4">
              <input type="password" name="password" class="form-control">
            </div>
            <div class="col-sm-4 info"></div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">确认密码</label>
            <div class="col-sm-4">
              <input type="password" name="repassword" class="form-control" >
            </div>
            <div class="col-sm-4 info"></div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">验证码</label>
            <div class="col-sm-4">
              <input type="password" name="password" class="form-control">
            </div>
            <div class="col-sm-4 info"></div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-2">
              <button type="button" class="btn btn-success">注册</button>
            </div>
            <div class="col-sm-4 info">
              <img width="100" src="index.php?m=index&c=reg&a=code" onclick="this.src='index.php?m=index&c=reg&a=code&num='+Math.random()" alt="">
            </div>
          </div>
        </form>
      </div>
      <hr>

      <footer>
        <p>&copy; 2016 Company, Inc.</p>
      </footer>
    </div>
  </body>

   <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

   <script>
     
      $('input').blur(function(event) {

          var  that =$(this);
          var value = $(this).val();

          var key = $(this).attr('name');

          if(key=='repassword'){

            if(value==$('#password').val()){
                that.parent().siblings('.info').html(' <p class="ok"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>两次密码一致</p>');
            }else{
              that.parent().siblings('.info').html(' <p class="error"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>两次密码不一致</p>');
            }
            return;
          }

          $.ajax({
            url: './index.php?c=user&a=check',
            type: 'POST',
            dataType: 'json',
            data: {value: value,key:key},
          })
          .done(function(res) {

            if(res.code){
                that.parent().siblings('.info').html(' <p class="error"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>'+res.info+'</p>');
            }else{
                that.parent().siblings('.info').html(' <p class="ok"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>'+res.info+'</p>');
            }
          })
          .fail(function() {
            console.log("error");
          })
          .always(function() {
            console.log("complete");
          });
          
      });

      $('button').click(function(event) {

          if($('.ok').length<4){
              $(this).parent().siblings('.info').html('<p class="error"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>数据不完整</p>');
             return;
          }
          
          $.ajax({
            url: './index.php?c=user&a=add',
            type: 'POST',
            dataType: 'json',
            data: $('#reg_form').serialize(),
          })
          .done(function(res) {
            if(res.code){
              $('button').parent().siblings('.info').html('<p class="error"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>'+res.info+'</p>');$('button').parent().siblings('.info').html('<p class="error"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>'+res.info+'</p>');
            }else{
              if(confirm("注册成功")){
                 location.href='./index.php';
              }
            }
          })
          .fail(function() {
            console.log("error");
          })
          .always(function() {
            console.log("complete");
          });
          
         
      });



   </script>
</html>
